package com.walala.logic;

import com.walala.logic.common.Global;
import com.walala.logic.dao.DataSourceManager;
import com.walala.logic.mq.KafkaConsumerClient;
import com.walala.logic.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

import com.walala.logic.common.Constant;
import com.walala.logic.server.Server;
import com.walala.logic.utils.ClassLoadUtils;
import com.walala.logic.utils.CommonUtils;

public class Main {

	private static Logger logger = LoggerFactory.getLogger(Main.class);

	static {
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(lc);
		lc.reset();
		try {
			configurator.doConfigure(CommonUtils.getRealPath() + "/config/logback.xml");
		} catch (JoranException e) {
		}
		StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
	}
	
	public static void main(String[] args) {
		
		final int port = 8000;
		
		//初始化消息处理器
		ClassLoadUtils.loadClass(Constant.MSG_HANDLER_PACK_PATH);
		
		new Thread(new Runnable() {
			
			public void run() {

				//初始化数据源
				try {
					DataSourceManager.getInstance().init();
				} catch (Exception e) {
					logger.error("init datasource error",e);
					System.exit(0);
				}

                //资源上报
				Resource.report();

				//启动服务
				new Server().start(Global.server_ip,Global.server_port);
			}
		}).start();

		//启动kafka消息消费
		new Thread(new Runnable() {
			@Override
			public void run() {
				KafkaConsumerClient.getInstance().start();
			}
		}).start();
	}
	
}
